﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Hartley.Entities;
using Hartley.DTOs;
namespace Hartley.BusinessLogic
{
    public class StudentManager
    {
        HCPPADataContext hcDc;
        const int maxNoOfHeadPrefect = 1;
        const int maxNoOfDeputyPrefect = 1;
        public StudentManager(HCPPADataContext hcDc)
        {
            this.hcDc = hcDc;
        }
        public void SaveStudent(StudentDTO studentDTO)
        {
            var studnt = hcDc.hcStudents.Where(n => n.StudentId == studentDTO.StudentId).SingleOrDefault();
            if (studnt == null)
            {
                hcStudent student = new hcStudent { StudentId = studentDTO.StudentId,
                                                    FirstName=studentDTO.FirstName,
                                                    LastName=studentDTO.LastName,
                                                    IndexNo=studentDTO.IndexNo,
                                                    House=studentDTO.House,
                                                    DateOfBirth = Convert.ToDateTime(studentDTO.DateOfBirth),
                                                    Address=studentDTO.Address,
                                                    OrdinaryBatchYear=studentDTO.OrdinaryBatchYear,
                                                    Description=studentDTO.Description,
                                                    PhotoPath=studentDTO.PhotoPath,
                                                    ModifiedBy = studentDTO.ModifiedBy,
                                                    ModifiedTime = studentDTO.ModifiedTime };
                hcDc.hcStudents.InsertOnSubmit(student);

            }

            hcDc.SubmitChanges();
        }


        

        /*public void SaveScholarships(ScholarshipDTO scholarshipDTO)
        {
            var scholar = hcDc.hcScholarships.Where(n => n.ScholarshipId == scholarshipDTO.ScholarshipId ).SingleOrDefault();
            if (scholar == null)
            {
                var scholName = hcDc.hcScholarships.Where(a => a.Title == scholarshipDTO.Title).SingleOrDefault();
                if (scholName == null)
                {

                    hcScholarship scholaship = new hcScholarship
                    {
                        ScholarshipId = scholarshipDTO.ScholarshipId,
                        Title = scholarshipDTO.Title,
                        Description = scholarshipDTO.Description,
                        OfferedBy = scholarshipDTO.OfferedBy,
                        InMemorialOf = scholarshipDTO.InMemorialOf,
                        Condition = scholarshipDTO.Condition,
                        Value = Convert.ToDecimal(scholarshipDTO.Value),

                    };
                    hcDc.hcScholarships.InsertOnSubmit(scholaship);
                }
                else
                {
                    //Don't add schol with same name
                }

            }
            else
            {
                //edit
            }

            hcDc.SubmitChanges();
        }*/


            public string SaveALResults(ALResultsDTO aLResultsDTO)
            {
                string result = "";
                var alrslt = hcDc.hcALevelResults.Where(n => n.ALevelResultId == aLResultsDTO.ALevelResultId).SingleOrDefault();
                try
                {
                if (alrslt == null)
                {
                    var alreslt = hcDc.hcALevelResults.Where(n => n.StudentId == aLResultsDTO.StudentId && n.Year == aLResultsDTO.Year).SingleOrDefault();

                    if (alreslt == null)
                    {
                   
                            hcALevelResult alResult = new hcALevelResult
                            {
                                ALevelResultId = aLResultsDTO.ALevelResultId,
                                StudentId = aLResultsDTO.StudentId,
                                Stream = aLResultsDTO.Stream,
                                NoOfAs = aLResultsDTO.NoOfAs,
                                NoOfBs = aLResultsDTO.NoOfBs,
                                NoOfCs = aLResultsDTO.NoOfCs,
                                NoOfSs = aLResultsDTO.NoOfSs,
                                NoOfFs = aLResultsDTO.NoOfFs,
                                DistrictRank = aLResultsDTO.DistrictRank,
                                IslandRank = aLResultsDTO.IslandRank,
                                Notes = aLResultsDTO.Notes,
                                Aggregate = aLResultsDTO.Aggregate,
                                Year = aLResultsDTO.Year,
                                // ModifiedBy = aLResultsDTO.ModifiedBy,
                                ModifiedTime = aLResultsDTO.ModifiedTime,
                            };
                            hcDc.hcALevelResults.InsertOnSubmit(alResult);

                            result = "Saved";
                        }
                   

               
                    else
                    {
                        //Students details exists for this year
                        result = "AlreadyExists";
                    }
                }

                else
                {
                    //Update
                    result = "Updated";
                }

                hcDc.SubmitChanges();

             }
             catch (Exception ee)
              {
                  throw;
              }
                return result;
            }


        public string SaveOLResults(OLResultsDTO oLResultsDTO)
        {
            string result = "";
            var olrslt = hcDc.hcOLevelResults.Where(n => n.OLevelResultId == oLResultsDTO.OLevelResultId).SingleOrDefault();
            try
            {

                if (olrslt == null)
                {
                    var olreslt = hcDc.hcOLevelResults.Where(n => n.StudentId == oLResultsDTO.StudentId && n.Year == oLResultsDTO.Year).SingleOrDefault();

                    if (olreslt == null)
                    {
                        hcOLevelResult olResult = new hcOLevelResult
                        {
                            OLevelResultId = oLResultsDTO.OLevelResultId,
                            StudentId = oLResultsDTO.StudentId,

                            NoOfAs = oLResultsDTO.NoOfAs,
                            NoOfBs = oLResultsDTO.NoOfBs,
                            NoOfCs = oLResultsDTO.NoOfCs,
                            NoOfSs = oLResultsDTO.NoOfSs,
                            NoOfFs = oLResultsDTO.NoOfFs,

                            Notes = oLResultsDTO.Notes,

                            Year = oLResultsDTO.Year,
                            ModifiedBy = oLResultsDTO.ModifiedBy,
                            ModifiedTime = oLResultsDTO.ModifiedTime,
                        };
                        hcDc.hcOLevelResults.InsertOnSubmit(olResult);

                        result = "Saved";

                    }
                    else
                    {
                        //Students details exists for this year
                        result = "AlreadyExists";
                    }
                }

                else
                {
                    //Update
                    result = "Updated";
                }


                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }
            return result;
        }


        public string SavePrefectBatch(PrefectBatchDTO prefectBatchDTO)
        {
            string result = "";
            var pfctBtch = hcDc.hcPrefectBatches.Where(n => n.PrefectBatchId == prefectBatchDTO.PrefectBatchId).SingleOrDefault();
            try
            {
                if (pfctBtch == null)
                {
                    var pfctBAtchName = hcDc.hcPrefectBatches.Where(a=>a.PrefectBatchName == prefectBatchDTO.PrefectBatchName).SingleOrDefault();

                    if(pfctBAtchName==null)
                    {
                    hcPrefectBatch prefectBatch = new hcPrefectBatch
                      {
                          PrefectBatchId = prefectBatchDTO.PrefectBatchId,
                          PrefectBatchName = prefectBatchDTO.PrefectBatchName,
                          Description = prefectBatchDTO.Description,
                          PrefectsFrom = prefectBatchDTO.PrefectFrom,
                          PrefectsTo = prefectBatchDTO.PrefectTo,
                          PrefectBatchPhotoName = prefectBatchDTO.PrefectBatchPhotoName,
                          PrefectBatchPhotoPath = prefectBatchDTO.PrefectBatchPhotoPath,
                          //ModifiedBy="tharsan",
                          ModifedTime = prefectBatchDTO.ModifiedTime

                      };
                    hcDc.hcPrefectBatches.InsertOnSubmit(prefectBatch);
                    result = "Saved";
                    }
                
                 else
                    {
                        //Students details exists for this year
                        result = "AlreadyExists";
                    }
                }

                else
                {
                    //Update
                    result = "Updated";
                }

                hcDc.SubmitChanges();
            }
            catch
            {
                throw;
            }

            return result;
        }


        public string SavePrefects(PrefectDTO prefectDTO)
        {
            string result = "";
            bool insertPrefect = false;
            var pfcts = hcDc.hcPrefects.Where(n => n.PrefectBatchId == prefectDTO.PrefecBatchtId && n.StudentId == prefectDTO.StudentId ).SingleOrDefault();
            try
            {
                string prefctBtchStart = hcDc.hcPrefectBatches.Where(a => a.PrefectBatchId == prefectDTO.PrefecBatchtId).FirstOrDefault().PrefectsFrom.ToString();
                string prefctBtchEnd = hcDc.hcPrefectBatches.Where(a => a.PrefectBatchId == prefectDTO.PrefecBatchtId).FirstOrDefault().PrefectsTo.ToString();

                DateTime prefctBtchStartDte = Convert.ToDateTime(prefctBtchStart);
                DateTime prefctBtchEndeDt = Convert.ToDateTime(prefctBtchEnd);


                if (pfcts == null)
                {
                    if (prefectDTO.Role == "Head Prefect")
                    {
                        var varHdPfct = hcDc.hcPrefects.Where(c => c.PrefectBatchId == prefectDTO.PrefecBatchtId && c.Role == "Head Prefect").ToList().Count;
                        if (varHdPfct < maxNoOfHeadPrefect)
                        {
                            insertPrefect = true;
                        }
                        else
                        {
                            // result = "NoOfMembersForThisRoleExceeded";
                            insertPrefect = false;
                        }
                    }

                    else if (prefectDTO.Role == "Deputy Head Prefect")
                    {
                        var varDptyHdPfct = hcDc.hcPrefects.Where(c => c.PrefectBatchId == prefectDTO.PrefecBatchtId && c.Role == "Deputy Head Prefect").ToList().Count;

                        if (varDptyHdPfct < maxNoOfDeputyPrefect)
                        {
                            insertPrefect = true;
                        }
                        else
                        {
                            //result = "NoOfMembersForThisRoleExceeded";
                            insertPrefect = false;
                        }
                    }

                    else if (prefectDTO.Role == "Senior Prefect")
                    {
                        insertPrefect = true;
                    }

                    else if (prefectDTO.Role == "Senior Prefect")
                    {
                        insertPrefect = true;
                    }
                    else
                    {
                       // insertPrefect = true;
                    }

                    if (insertPrefect)
                    {
                        var pfctstrt = hcDc.hcPrefects.Where(a => a.PrefectBatchId == prefectDTO.PrefecBatchtId).FirstOrDefault();
                        var pfctstp = hcDc.hcPrefects.Where(a => a.PrefectBatchId == prefectDTO.PrefecBatchtId).FirstOrDefault();
                        string prfctBtchStart = pfctstrt != null ? pfctstrt.PrefectFrom.ToString() : prefectDTO.PrefectFrom.ToString();
                        string prfctBtchEnd = pfctstp != null ? pfctstp.PrefectTo.ToString() : prefectDTO.PrefectTo.ToString(); 

                      
                            //If it's nt null then compare with the ccommiitee start & end data to make sure member''s period exceed committee period
                        DateTime prfctBtchStartDate = Convert.ToDateTime(prfctBtchStart);
                        DateTime prfctBtchEndDate = Convert.ToDateTime(prfctBtchEnd);


                        if (prefectDTO.PrefectFrom < prfctBtchStartDate || prefectDTO.PrefectTo > prfctBtchEndDate)
                        {
                            result = "InvalidPrefectPeriod";
                        }
                        else
                        {
                            hcPrefect prefect = new hcPrefect
                           {
                               PrefectBatchId = prefectDTO.PrefecBatchtId,
                               StudentId = prefectDTO.StudentId,
                               Notes = prefectDTO.Description,
                               PrefectFrom = prefectDTO.PrefectFrom,
                               PrefectTo = prefectDTO.PrefectTo,
                               Role = prefectDTO.Role,

                               //ModifiedBy="tharsan",
                               ModifiedAt = Convert.ToDateTime( prefectDTO.ModifiedTime)

                           };
                            hcDc.hcPrefects.InsertOnSubmit(prefect);
                            result = "Saved";
                        }

                    }

                     else
                    {
                        //Cannot insert more than 2 head or 2 dpty head
                        result = "NoOfStudentsForThisRoleExceeded";
                    }
                }
            
                else
                    {
                        //edit
                        result = "Updated";
                    }
            

                    hcDc.SubmitChanges();
                }
            
            catch
            {
                throw;
            }
            return result;
        }

        public List<StudentDTO> GetStudents()
        {
            return hcDc.hcStudents.Select(s => new StudentDTO
            {
                StudentId = s.StudentId,
                FullName = s.LastName + "  " + s.FirstName,
                Address = s.Address,
                DateOfBirth = s.DateOfBirth,
                Description = s.Description,
                FirstName = s.FirstName,
                House = s.House,
                IndexNo = s.IndexNo,
                LastName = s.LastName,
                OrdinaryBatchYear = s.OrdinaryBatchYear,
                PhotoPath = s.PhotoPath
            }).ToList();

        }

        public List<StudentDTO> GetStudentById(Guid studentId)
        {
            return hcDc.hcStudents.Where(a => a.StudentId == studentId).Select(s => new StudentDTO
            {
                StudentId = s.StudentId,
                FullName = s.LastName + "  " + s.FirstName,
                Address = s.Address,
                DateOfBirth = s.DateOfBirth,
                Description = s.Description,
                FirstName = s.FirstName,
                House = s.House,
                IndexNo = s.IndexNo,
                LastName = s.LastName,
                OrdinaryBatchYear = s.OrdinaryBatchYear,
                PhotoPath = s.PhotoPath
            }).ToList();

        }

        public List<StudentDTO> GetStudentByOLYear(string olYear)
        {
            return hcDc.hcStudents.Where(a => a.OrdinaryBatchYear == olYear).Select(s => new StudentDTO
            {
                StudentId = s.StudentId,
                FullName = s.LastName + "  " + s.FirstName,
                Address = s.Address,
                DateOfBirth = s.DateOfBirth,
                Description = s.Description,
                FirstName = s.FirstName,
                House = s.House,
                IndexNo = s.IndexNo,
                LastName = s.LastName,
                OrdinaryBatchYear = s.OrdinaryBatchYear,
                PhotoPath = s.PhotoPath
            }).ToList();

        }


        public List<OLResultsDTO> GetAllOLevelResults()
        {
            return hcDc.hcOLevelResults.Select(s => new OLResultsDTO
            {
                StudentId = s.StudentId,
                OLevelResultId = s.OLevelResultId ,
                NoOfAs = s.NoOfAs,
                NoOfBs = s.NoOfBs,
                NoOfCs = s.NoOfCs,
                NoOfFs = s.NoOfFs,
                NoOfSs = s.NoOfSs,
                Notes = s.Notes,
                NavigationUrl ="",
                Year = s.Year == null ? 0 : Convert.ToInt32(s.Year) 
                
            }).ToList();

        }

        public List<OLResultsDTO> GetOLevelResultByYear(int olevelYear)
        {
            return hcDc.hcOLevelResults.Where(a=>a.Year == olevelYear).Select(s => new OLResultsDTO
            {
                StudentId = s.StudentId,
                OLevelResultId = s.OLevelResultId,
                NoOfAs = s.NoOfAs,
                NoOfBs = s.NoOfBs,
                NoOfCs = s.NoOfCs,
                NoOfFs = s.NoOfFs,
                NoOfSs = s.NoOfSs,
                Notes = s.Notes,
                NavigationUrl = "",
                Year = s.Year == null ? 0 : Convert.ToInt32(s.Year)

            }).ToList();

        }
        public List<ALResultsDTO> GetAllALevelResults()
        {
            return hcDc.hcALevelResults.Select(s => new ALResultsDTO
            {
                StudentId = s.StudentId,
                ALevelResultId = s.ALevelResultId,
                NoOfAs = Convert.ToInt32(s.NoOfAs),
                NoOfBs = Convert.ToInt32(s.NoOfBs),
                NoOfCs = Convert.ToInt32(s.NoOfCs),
                NoOfFs = Convert.ToInt32(s.NoOfFs),
                NoOfSs = Convert.ToInt32(s.NoOfSs),
                Notes = s.Notes,
                NavigationUrl = "",
                Stream = s.Stream,
                Aggregate = s.Aggregate,
                IslandRank = s.IslandRank,
                DistrictRank = s.DistrictRank,
                Year = Convert.ToInt32(s.Year)

            }).ToList();

        }

        public List<ALResultsDTO> GetALevelResultByYear(int alevelYear)
        {
           // List<ALResultsDTO> alResultDTO = new List<ALResultsDTO>();
            //alResultDTO.AddRange();
            return hcDc.hcALevelResults.Where(a=>a.Year == alevelYear).Select(s => new ALResultsDTO
            {
                StudentId = s.StudentId,
                ALevelResultId = s.ALevelResultId,
                NoOfAs = Convert.ToInt32(s.NoOfAs),
                NoOfBs = Convert.ToInt32(s.NoOfBs),
                NoOfCs = Convert.ToInt32(s.NoOfCs),
                NoOfFs = Convert.ToInt32(s.NoOfFs),
                NoOfSs = Convert.ToInt32(s.NoOfSs),
                Notes = s.Notes,
                NavigationUrl = "",
                Stream = s.Stream,
                Aggregate = s.Aggregate,
                IslandRank = s.IslandRank,
                DistrictRank = s.DistrictRank,
                Year = Convert.ToInt32(s.Year)

            }).ToList();

        }


        public List<PrefectBatchDTO> GetAllPrefectBatches()
        {
            return hcDc.hcPrefectBatches.Select(s => new PrefectBatchDTO
            {
                PrefectBatchId = s.PrefectBatchId,
                PrefectBatchName = s.PrefectBatchName,
                PrefectFrom = s.PrefectsFrom,
                PrefectTo = s.PrefectsTo,
                Description = s.Description,
               PrefectBatchPhotoName = s.PrefectBatchPhotoName,
               PrefectBatchPhotoPath = s.PrefectBatchPhotoPath,
               ModifiedBy = s.ModifiedBy,
               ModifiedTime = s.ModifedTime
            }).ToList();

        }

        public List<PrefectBatchDTO> GetPrefectBatchById(Guid prefectBatchId)
        {
            return hcDc.hcPrefectBatches.Where(a=>a.PrefectBatchId == prefectBatchId).Select(s => new PrefectBatchDTO
            {
                PrefectBatchId = s.PrefectBatchId,
                PrefectBatchName = s.PrefectBatchName,
                PrefectFrom = s.PrefectsFrom,
                PrefectTo = s.PrefectsTo,
                Description = s.Description,
                PrefectBatchPhotoName = s.PrefectBatchPhotoName,
                PrefectBatchPhotoPath = s.PrefectBatchPhotoPath,
                ModifiedBy = s.ModifiedBy,
                ModifiedTime = s.ModifedTime
            }).ToList();

        }

        public List<PrefectDTO> GetPrefectsByPrefectBatchId(Guid prefectBatchId)
        {
            List<PrefectDTO> prefectsDTO = new List<PrefectDTO>();

            //First add head prefect & then deputy & seniors then it will be ordered in tht ordr
            prefectsDTO.AddRange(hcDc.hcPrefects.Where(a => a.PrefectBatchId == prefectBatchId && a.Role == "Head Prefect").Select(s => new PrefectDTO
            {
                PrefecBatchtId = s.PrefectBatchId,
                StudentId = s.StudentId,
                PrefectFrom = s.PrefectFrom,
                PrefectTo = s.PrefectTo,
                Description = s.Notes,
                Role = s.Role,
                ModifiedBy = s.ModifiedBy,
                ModifiedTime = s.ModifiedAt
            }).ToList());


            prefectsDTO.AddRange(hcDc.hcPrefects.Where(a => a.PrefectBatchId == prefectBatchId && a.Role == "Deputy Head Prefect").Select(s => new PrefectDTO
            {
                PrefecBatchtId = s.PrefectBatchId,
                StudentId = s.StudentId,
                PrefectFrom = s.PrefectFrom,
                PrefectTo = s.PrefectTo,
                Description = s.Notes,
                Role = s.Role,
                ModifiedBy = s.ModifiedBy,
                ModifiedTime = s.ModifiedAt
            }).ToList());

            prefectsDTO.AddRange(hcDc.hcPrefects.Where(a => a.PrefectBatchId == prefectBatchId && a.Role == "Senior Prefect").Select(s => new PrefectDTO
            {
                PrefecBatchtId = s.PrefectBatchId,
                StudentId = s.StudentId,
                PrefectFrom = s.PrefectFrom,
                PrefectTo = s.PrefectTo,
                Description = s.Notes,
                Role = s.Role,
                ModifiedBy = s.ModifiedBy,
                ModifiedTime = s.ModifiedAt
            }).ToList());

            prefectsDTO.AddRange(hcDc.hcPrefects.Where(a => a.PrefectBatchId == prefectBatchId && a.Role == "Other").Select(s => new PrefectDTO
            {
                PrefecBatchtId = s.PrefectBatchId,
                StudentId = s.StudentId,
                PrefectFrom = s.PrefectFrom,
                PrefectTo = s.PrefectTo,
                Description = s.Notes,
                Role = s.Role,
                ModifiedBy = s.ModifiedBy,
                ModifiedTime = s.ModifiedAt
            }).ToList());

            return prefectsDTO;

        }

        public List<PrefectDTO> GetAllPrefects()
        {


            //First add head prefect & then deputy & seniors then it will be ordered in tht ordr
            return hcDc.hcPrefects.Select(s => new PrefectDTO
               {
                   PrefecBatchtId = s.PrefectBatchId,
                   StudentId = s.StudentId,
                   PrefectFrom = s.PrefectFrom,
                   PrefectTo = s.PrefectTo,
                   Description = s.Notes,
                   Role = s.Role,
                   ModifiedBy = s.ModifiedBy,
                   ModifiedTime = s.ModifiedAt
               }).ToList();
        }


        public List<PrefectDTO> GetPrefectsById(Guid prefectId)
        {


            //First add head prefect & then deputy & seniors then it will be ordered in tht ordr
            return hcDc.hcPrefects.Where(a=>a.StudentId == prefectId).Select(s => new PrefectDTO
            {
                PrefecBatchtId = s.PrefectBatchId,
                StudentId = s.StudentId,
                PrefectFrom = s.PrefectFrom,
                PrefectTo = s.PrefectTo,
                Description = s.Notes,
                Role = s.Role,
                ModifiedBy = s.ModifiedBy,
                ModifiedTime = s.ModifiedAt
            }).ToList();
        }
    }
}
